package com.boe.csb.core.entity;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class UserSession {

    private static Logger logger = LoggerFactory.getLogger(UserSession.class);

    private String sessionId;
    private Long id;
    private Long boeUserId;
    private String deviceId;


    public UserSession(Long id, Long boeUserId, String deviceId) {
        this.id = id;
        this.boeUserId = boeUserId;
        this.deviceId = deviceId;
        genSessionId();
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getBoeUserId() {
        return boeUserId;
    }

    public void setBoeUserId(Long boeUserId) {
        this.boeUserId = boeUserId;
    }

    private String genSessionId() {
        if (this.id != null && this.boeUserId != null && this.deviceId != null) {
            String original = this.id + this.boeUserId + this.deviceId;
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(original.getBytes());
                byte[] digest = md.digest();
                this.setSessionId(String.valueOf(Base64.encodeBase64(digest)));
                return this.getSessionId();
            } catch (NoSuchAlgorithmException e) {
                logger.error("genSessionId"+e.toString());
            }
        }
        return null;
    }


    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
}
